import * as express from 'express'
import { Request, Response, NextFunction } from 'express'
import config from './config/index'
import { Fail } from './app/common/Back'
import router from './route/index'
import { createConnection } from "typeorm";

// 创建 typeorm 连接
createConnection().then(connection => {
  const app = express()
// 注册路由
  app.use(router)

// 全局错误拦截
  app.use((err: Fail, req: Request, res: Response, next: NextFunction) => {
    res.json({ code: err.code, message: err.message })
  })

  app.listen(config.port, (): void => {
    console.log(`Example app listening at http://localhost:${config.port}`)
  })
})
